import java.util.ArrayList;
import java.util.HashMap;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * This class contains all methods to build and
 * return (not sending though) a valid HTTP header.
 *
 * @author Marlon Etheredge
 * @author <a href="mailto:m.etheredge@gmail.com">m.etheredge@gmail.com</a>
 * @author <a href="http://www.foxage.nl/">http://www.foxage.nl</a>
 */
public final class HttpHeader {
    
	/** Flag set to true whenever the Connection should be closed. */
	private boolean httpConnectionCloseFlag = true;
	
	/** HTTP Connection header field. */
	private String httpConnection = "Connection: close";
	
	/** HTTP Content-Type header field. */
	private String httpContentType;
	
	/** HTTP Content-Length header field. */
	private String httpContentLength;
	
	/** HTTP Server header field. */
	private final String httpServer = "Server: " + Webster.getHttpServer();
	
	/** Constructs a new empty HttpHeader instance. */
	public HttpHeader() {}
	
	/** The statusline */
	private String statusLine;
	
	/** The Last Modified Date */
	private String lastModified;
	
	/** 
	 * Builds and returns the HttpHeader.
	 *
	 * @return byte[] Byte[] containing the built HttpHeader
	 */
	public byte[] build() {
		String httpHeader = "";
		
		httpHeader += "HTTP/1.x " + this.statusLine + "\r\n";
		httpHeader += "Date: " + this.getCurrentTimestamp() + "\r\n";
		httpHeader += this.httpServer + "\r\n";
		
		if(this.httpConnection != null) {
			httpHeader += this.httpConnection + "\r\n";
		}
		
		if(this.httpContentType != null) {
			httpHeader += this.httpContentType + "\r\n";
		}
		
		if(this.httpContentLength != null) {
			httpHeader += this.httpContentLength + "\r\n";
		}
		
		if(this.lastModified != null) {
			httpHeader += this.lastModified + "\r\n";
		}
		
		httpHeader += "\r\n";
		
		return httpHeader.getBytes();
	}
	
	/**
	 * Sets the httpConnection with httpConnection
	 * parameter and prefix 'Connection: '.
	 *
	 * @param httpConnection HttpConnection field to set
	 */
	public void setHttpConnection(String httpConnection) {
		if(httpConnection.toLowerCase().equals("close"))
			this.httpConnectionCloseFlag = true;
		
		this.httpConnection = "Connection: " + httpConnection;
	}
	
	/** 
	 * Returns the httpConnectionCloseFlag. 
	 *
	 * @return The httpConnectionCloseFlag
	 */
	public boolean getHttpConnectionCloseFlag() {
		return this.httpConnectionCloseFlag;
	}
	
	/**
	 * Sets the httpContentType with httpContentType
	 * parameter and prefix 'Content-Type: '.
	 *
	 * @param httpContentType HttpContentType field to set
	 */
	public void setHttpContentType(String httpContentType) {
		this.httpContentType = "Content-Type: " + httpContentType;
	}
	
	/**
	 * Sets the httpContentLength with httpContentLength
	 * parameter and prefix 'Content-Length: '.
	 *
	 * @param httpContentLength HttpContentLength field to set
	 */
	public void setHttpContentLength(int httpContentLength) {
		this.httpContentLength = "Content-Length: " + httpContentLength;
	}
	
	/**
	 * Returns the httpContentType
	 *
	 * @return String The httpContentType
	 */
	public String getHttpContentType() {
		return this.httpContentType;
	}
	
	/**
	 * Sets the Statusline with statusline parameter.
	 *
	 * @param statusLine to set
	 */
	public void setStatusLine(String statusLine) {
		this.statusLine = statusLine;
	}
	
	/**
	 * Sets the Last Modified date for this HttpHeader's body content.
	 *
	 * @param lastModified Date object for Last Modified date.
	 */
	public void setLastModified(Date lastModified) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
		
		this.lastModified = "Last-Modified: " + dateFormat.format(lastModified);
	}
	
	/**
	 * Returns a current Timestamp in the form as String
	 * in format: "EEE, d MMM yyyy HH:mm:ss z".
	 *
	 * @see http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
	 * @return Current Timestamp
	 */
	private String getCurrentTimestamp() {
		Date currentDate = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
		
		return dateFormat.format(currentDate);
	}

}
